package com.cos.m3.web.controller;

import com.cos.m3.util.FileUtil;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author: COS
 * @time: 2022/6/9 11:21
 * @description:
 */
@Controller
@RequestMapping("/file")
public class FileController {


    @RequestMapping(value = "/upload", produces = "text/html;charset=utf-8")
    @ResponseBody
    public String upload(MultipartFile file, HttpServletRequest request) throws IOException {
        //设置黑名单
        List<String> blackList = new ArrayList<>();
        blackList.add("革命");
        blackList.add("黄");
        blackList.add("赌");
        blackList.add("毒");
        blackList.add("汪智");
        blackList.add("和蟹神兽");
        String filename = file.getOriginalFilename();
        System.out.println(filename);
        for (String s : blackList) {
            if (filename.contains(s)) {
                return "我已通知网警,等死吧";
            }
        }
        System.out.println(file.getSize());
        if (file.getSize() > 204800) {
            return "文件过大,没钱不干";
        }
        System.out.println(file.getContentType());
        String newFileName = UUID.randomUUID().toString() + "_" + filename;
        //获取服务器的路径
        //项目在服务器的部署路径
        //散列存储
        // 0-15的多级路径
        //upload/0/5/344ddfd5-78fa-46a1-bfbc-0033dbdf0628_汪智.pptx
        //upload/1/4/344ddfd5-78fa-46a1-bfbc-0033dbdf0628_汪智.pptx
        String realPath = request.getServletContext().getRealPath("/upload");
        File filePath = new File(realPath);
        if (!filePath.exists()) {
            //创建目录
            filePath.mkdir();
        }
        //保存文件
        file.transferTo(new File(realPath + "/" + newFileName));
        return "ok";
    }

    @RequestMapping("/download")
    public void downLoad(String fileName, HttpServletResponse response, HttpServletRequest request) throws IOException {
        //找到文件下载的路径
        String realPath = request.getServletContext().getRealPath("/upload");
        //找到文件具体路径
        String newPath = realPath + "/" + fileName;
        //4b1c88a4-f490-4cb3-8157-48b134cf0091_zw.pptx
        fileName = fileName.split("_")[1];
        //设置响应头
        response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8"));
        //把文件响应出去
        IOUtils.copy(new FileInputStream(newPath), response.getOutputStream());
    }

    @RequestMapping("/getList")
    public String getFileList(Model model, HttpServletRequest request) {
        String realPath = request.getServletContext().getRealPath("/upload");
        List<String> fileNames = FileUtil.getFileNames(new File(realPath));
        model.addAttribute("list", fileNames);
        return "downList";
    }

}
